/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2012 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id: header 321634 2012-01-01 13:15:04Z felipe $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mydbc.h"

/* If you declare any globals in php_mydbc.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(mydbc)
*/

/* True global resources - no need for thread safety here */
static int le_mydbc;

/* True global resources - no need for thread safety here */
static char *ht_rdir = "";
static zend_class_entry* mydbc_class_entry;
static zend_object_handlers mydbc_object_handlers;

/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO(arginfo_mydbc_open, 0)
	ZEND_ARG_INFO(0, surl)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_close, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_query, 0)
	ZEND_ARG_INFO(0, ssql)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_query_one, 0)
	ZEND_ARG_INFO(0, ssql)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_query_row, 0)
	ZEND_ARG_INFO(0, ssql)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_query_all, 0)
	ZEND_ARG_INFO(0, ssql)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_version, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_construct, 0)
ZEND_ARG_INFO(0, surl)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_mydbc_destruct, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mydbc_functions[]
 *
 * Every user visible function must have an entry in mydbc_functions[].
 */
const zend_function_entry mydbc_functions[] = {
	PHP_ME(mydbc, open, arginfo_mydbc_open, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, close, arginfo_mydbc_close, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, query, arginfo_mydbc_query, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, query_one, arginfo_mydbc_query_one, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, query_row, arginfo_mydbc_query_row, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, query_all, arginfo_mydbc_query_all, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, version, arginfo_mydbc_version, ZEND_ACC_PUBLIC)
	PHP_ME(mydbc, __construct, arginfo_mydbc_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
	PHP_ME(mydbc, __destruct, arginfo_mydbc_destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
	//PHP_FE(theme, NULL)
	PHP_FE_END	/* Must be the last line in mydbc_functions[] */
};
/* }}} */

/* {{{ mydbc_module_entry
 */
zend_module_entry mydbc_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"mydbc",
	mydbc_functions,
	PHP_MINIT(mydbc),
	PHP_MSHUTDOWN(mydbc),
	PHP_RINIT(mydbc),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(mydbc),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(mydbc),
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_MYDBC
ZEND_GET_MODULE(mydbc)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("mydbc.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_mydbc_globals, mydbc_globals)
    STD_PHP_INI_ENTRY("mydbc.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_mydbc_globals, mydbc_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_mydbc_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_mydbc_init_globals(zend_mydbc_globals *mydbc_globals)
{
	mydbc_globals->global_value = 0;
	mydbc_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(mydbc)
{
	/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/
	zend_class_entry class_entry;
	INIT_CLASS_ENTRY(class_entry, "mydbc", mydbc_functions);
	mydbc_class_entry = zend_register_internal_class(&class_entry TSRMLS_CC);

	zend_declare_property_null(mydbc_class_entry, ZEND_STRL("conn"), ZEND_ACC_PRIVATE TSRMLS_CC);
	zend_declare_property_string(mydbc_class_entry, ZEND_STRL("dburl"), "", ZEND_ACC_PUBLIC TSRMLS_CC);
	zend_declare_property_string(mydbc_class_entry, ZEND_STRL("charset"), "utf8", ZEND_ACC_PUBLIC TSRMLS_CC);
	zend_declare_property_string(mydbc_class_entry, ZEND_STRL("timezone"), "+8:00", ZEND_ACC_PUBLIC TSRMLS_CC);
	
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(mydbc)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(mydbc)
{
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(mydbc)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(mydbc)
{
	php_info_print_table_start();
	php_info_print_table_row(2, "mydbc support", "enabled");
	php_info_print_table_row(2, "mydbc version", PHP_MYDBC_VERSION);
	php_info_print_table_end();
	
	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

PHP_METHOD(mydbc, open) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */

PHP_METHOD(mydbc, close) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */

PHP_METHOD(mydbc, query) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */

PHP_METHOD(mydbc, query_one) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */

PHP_METHOD(mydbc, query_row) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */

PHP_METHOD(mydbc, query_all) /* {{{ */
{
	RETURN_TRUE;
}
/* }}} */


PHP_METHOD(mydbc, version) /* {{{ */
{
	if (ZEND_NUM_ARGS() != 0) {
		WRONG_PARAM_COUNT;
	}
	
	RETURN_STRING(PHP_MYDBC_VERSION, 1);
}
/* }}} */

PHP_METHOD(mydbc, __construct) /* {{{ */
{
	char *surl = NULL;
	int surl_len = 0;
	
	if (ZEND_NUM_ARGS() > 1) {
		WRONG_PARAM_COUNT;
	}

	if (ZEND_NUM_ARGS() == 1) {
		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &surl, &surl_len) == FAILURE) {
			 WRONG_PARAM_COUNT;
		}
		
		mydbc_open(getThis(), surl);
	}
}
/* }}} */

PHP_METHOD(mydbc, __destruct) /* {{{ */
{

}
/* }}} */

// mydbc_open
static void *mydbc_open(zval *object, char *dburl) /* {{{ */
{
	TSRMLS_FETCH();
	
	zend_update_property_string(mydbc_class_entry, object, ZEND_STRL("dburl"), dburl TSRMLS_CC);

}
/* }}} */
	
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
